package com.ruoyi.insurance.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.insurance.domain.ClaimRecord;
import com.ruoyi.insurance.mapper.InsuranceContractMapper;
import com.ruoyi.insurance.domain.InsuranceContract;
import com.ruoyi.insurance.service.IInsuranceContractService;

/**
 * 保险合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-09
 */
@Service
public class InsuranceContractServiceImpl implements IInsuranceContractService 
{
    @Autowired
    private InsuranceContractMapper insuranceContractMapper;

    /**
     * 查询保险合同
     * 
     * @param id 保险合同主键
     * @return 保险合同
     */
    @Override
    public InsuranceContract selectInsuranceContractById(Long id)
    {
        return insuranceContractMapper.selectInsuranceContractById(id);
    }

    /**
     * 查询保险合同列表
     * 
     * @param insuranceContract 保险合同
     * @return 保险合同
     */
    @Override
    public List<InsuranceContract> selectInsuranceContractList(InsuranceContract insuranceContract)
    {
        return insuranceContractMapper.selectInsuranceContractList(insuranceContract);
    }

    /**
     * 新增保险合同
     * 
     * @param insuranceContract 保险合同
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInsuranceContract(InsuranceContract insuranceContract)
    {
        int rows = insuranceContractMapper.insertInsuranceContract(insuranceContract);
        insertClaimRecord(insuranceContract);
        return rows;
    }

    /**
     * 修改保险合同
     * 
     * @param insuranceContract 保险合同
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInsuranceContract(InsuranceContract insuranceContract)
    {
        insuranceContractMapper.deleteClaimRecordByContractId(insuranceContract.getId());
        insertClaimRecord(insuranceContract);
        return insuranceContractMapper.updateInsuranceContract(insuranceContract);
    }

    /**
     * 批量删除保险合同
     * 
     * @param ids 需要删除的保险合同主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInsuranceContractByIds(Long[] ids)
    {
        insuranceContractMapper.deleteClaimRecordByContractIds(ids);
        return insuranceContractMapper.deleteInsuranceContractByIds(ids);
    }

    /**
     * 删除保险合同信息
     * 
     * @param id 保险合同主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInsuranceContractById(Long id)
    {
        insuranceContractMapper.deleteClaimRecordByContractId(id);
        return insuranceContractMapper.deleteInsuranceContractById(id);
    }

    /**
     * 新增索赔记录信息
     * 
     * @param insuranceContract 保险合同对象
     */
    public void insertClaimRecord(InsuranceContract insuranceContract)
    {
        List<ClaimRecord> claimRecordList = insuranceContract.getClaimRecordList();
        Long id = insuranceContract.getId();
        if (StringUtils.isNotNull(claimRecordList))
        {
            List<ClaimRecord> list = new ArrayList<ClaimRecord>();
            for (ClaimRecord claimRecord : claimRecordList)
            {
                claimRecord.setContractId(id);
                list.add(claimRecord);
            }
            if (list.size() > 0)
            {
                insuranceContractMapper.batchClaimRecord(list);
            }
        }
    }
}
